package com.healthtop.service.impl;

import com.healthtop.common.Specification;
import com.healthtop.dao.XsRoleDao;
import com.healthtop.dto.XsRoleDto;
import com.healthtop.model.XsRole;
import com.healthtop.service.XsRoleService;
import com.healthtop.utils.UserUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class XsRoleServiceImpl implements XsRoleService {

    @Autowired
    private XsRoleDao xsRoleDao;

    @Override
    public List<XsRole> selectRoleByUid(Long userId) {
        return null;
    }

    @Override
    public XsRoleDto getForwardValues(XsRoleDto xsRoleDto) {
       if(xsRoleDto.getMode()== Specification.OperateMode.UPDATE.index){
           XsRole xsRole = xsRoleDao.selectXsRoleById(xsRoleDto.getRoleId());
           BeanUtils.copyProperties(xsRole,xsRoleDto);
       }
        return xsRoleDto;
    }

    @Override
    public void operate(XsRoleDto xsRoleDto) {
        XsRole xsRole = new XsRole();
        BeanUtils.copyProperties(xsRoleDto,xsRole);
        if(Specification.OperateMode.CREATE.index==xsRoleDto.getMode()){
            xsRole.setCreateTime(new Date());
            xsRole.setCreateUser(UserUtils.getCurrentUser().getUid());
            xsRole.setDelFalg("0");
            xsRoleDao.insert(xsRole);
        }else{
            xsRole.setUpdateTime(new Date());
            xsRole.setUpdateUser(UserUtils.getCurrentUser().getUid());
            xsRoleDao.update(xsRole);
        }
    }

    @Override
    public void deleteRole(Long roleId) {
        XsRole xsRole = xsRoleDao.selectXsRoleById(roleId);
        xsRole.setDelFalg("1");
        xsRole.setDelTime(new Date());
        xsRoleDao.update(xsRole);
    }
}
